import { app,globalShortcut,Tray,nativeImage, shell,Menu, BrowserWindow, ipcMain } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import appIcon from '../../build/icon.png?asset'
let mainID =  null
let codeID = null

function createMain() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 550,
    height: 550,
    frame: false,
    transparent: true,
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      spellcheck: false,
      sandbox: false,
      contextIsolation: false,
    }
  })

  mainWindow.on('ready-to-show', () => {
    mainWindow.show()
  })
  mainID = mainWindow.id

  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  } else {
    mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  }
}

function createCode() {
  const codeWindow = new BrowserWindow({
    width: 1000,
    height: 500,
    frame: false,
    show: false,
    transparent: true,
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      spellcheck: false,
      contextIsolation: false,
    },
  });

  codeID = codeWindow.id

  ipcMain.on('setCodeHidden', (event,value) => {
    if (value){
      codeWindow.show()
    }else {
      codeWindow.hide()
    }
  })

  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    codeWindow.loadURL(process.env['ELECTRON_RENDERER_URL']+"/code")
  } else {
    codeWindow.loadFile(join(__dirname, '../renderer/index.html'),{hash:'code'})
  }
}

app.whenReady().then(() => {
  electronApp.setAppUserModelId('com.electron')

  globalShortcut.register('Alt+i', () => {
    console.log(__dirname)
  })
  globalShortcut.register('esc', () => {
    BrowserWindow.getFocusedWindow()?.hide()
  })


  let tray = new Tray(nativeImage.createFromPath(appIcon))
  const contextMenu = Menu.buildFromTemplate([
    { label: '主程序', type: 'normal',click:()=>{BrowserWindow.fromId(mainID).show()} },
    { label: '设置', type: 'normal' ,click:()=>{BrowserWindow.fromId(codeID).show()}},
    { label: '退出', type: 'normal',click:()=>{app.quit()} },
  ])
  tray.setToolTip('This is my application.')
  tray.setContextMenu(contextMenu)

  ipcMain.on('set-ignore-mouse-events', (event, ignore, options) => {
    const win = BrowserWindow.fromWebContents(event.sender)
    win.setIgnoreMouseEvents(ignore, options)
    console.log('set-ignore-mouse-events', ignore, options)
  })

  createMain()
  createCode()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
